package com.jinzhi.jzweb.controller;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.jinzhi.common.annotation.Log;
import com.jinzhi.common.base.BaseController;
import com.jinzhi.common.utils.Result;
import com.jinzhi.jzweb.domain.ExaminationBatchDO;
import com.jinzhi.jzweb.domain.ExaminationPointDO;
import com.jinzhi.jzweb.domain.OrganizationNewsDO;
import com.jinzhi.jzweb.service.ExaminationBatchService;
import com.jinzhi.jzweb.service.ExaminationPointService;
import com.jinzhi.jzweb.service.OrganizationNewsService;
import com.jinzhi.jzweb.service.OrganizationRolesService;
import com.jinzhi.sys.domain.UserDO;
import com.jinzhi.sys.service.UserService;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 
 * <pre>
 * 考点分配
 * </pre>
 * <small> 2020-02-24 21:00:12 | lance</small>
 */
@Controller
@RequestMapping("/jzweb/examinationPoint")
public class ExaminationPointController extends BaseController {
	@Autowired
	private ExaminationPointService examinationPointService;
	@Autowired
	private UserService userService;
	@Autowired
	private OrganizationNewsService organizationNewsService;
	@Autowired
	private OrganizationRolesService organizationRolesService;
	@Autowired
	private ExaminationBatchService examinationBatchService;
	
	@GetMapping()
	@RequiresPermissions("jzweb:examinationPoint:examinationPoint")
	String ExaminationPoint(){
	    return "jzweb/examinationPoint/examinationPoint";
	}
	
	@ResponseBody
	@GetMapping("/list")
	@RequiresPermissions("jzweb:examinationPoint:examinationPoint")
	public Result<Page<ExaminationPointDO>> list(HttpServletRequest req ,ExaminationPointDO examinationPointDTO){
		Wrapper<ExaminationPointDO> wrapper = new EntityWrapper<ExaminationPointDO>().orderBy("id", false);
		if (examinationPointDTO.getState() != null){
			wrapper.eq("state",examinationPointDTO.getState());
		}
		if (StringUtils.isNotBlank(examinationPointDTO.getEbatchName())){
			Wrapper<ExaminationBatchDO> exWrapper = new EntityWrapper<ExaminationBatchDO>().like("name",examinationPointDTO.getEbatchName());
			List<ExaminationBatchDO>  examinationBatchDOS = examinationBatchService.selectList(exWrapper);
			String str = "";
			for (ExaminationBatchDO examinationBatchDO : examinationBatchDOS){
				str = str + examinationBatchDO.getId() + ",";
			}
			wrapper.and();
			if(StringUtils.isNotBlank(str)){
				wrapper.in("ebatch_id",str.substring(0,str.length()-1));
			}else{
				wrapper.eq("ebatch_id",null);
			}
		}

		//查询用户角色
		String roleName = getRoleNames();
		if (!roleName.contains("超级用户角色")) {
			Object orgId = req.getSession().getAttribute("orgId");
			if (orgId != null){
				wrapper.eq("org_id",orgId);
			}else {
				wrapper.eq("org_id",this.getUser().getOrgId());
			}
		}else {
			if (StringUtils.isNotBlank(examinationPointDTO.getOrgName())){
				Wrapper<OrganizationNewsDO> orWrapper = new EntityWrapper<OrganizationNewsDO>().like("name",examinationPointDTO.getOrgName());
				List<OrganizationNewsDO>  organizationNewsDOS = organizationNewsService.selectList(orWrapper);
				String str = "";
				for (OrganizationNewsDO organizationNewsDO : organizationNewsDOS){
					str = str + organizationNewsDO.getId() + ",";
				}
				wrapper.and();
				if(StringUtils.isNotBlank(str)){
					wrapper.in("org_id",str.substring(0,str.length()-1));
				}else{
					wrapper.eq("org_id",null);
				}
			}
		}

		Page<ExaminationPointDO> page = examinationPointService.selectPage(getPage(ExaminationPointDO.class), wrapper);
		List list = new ArrayList();
		for (int i = 0; i < page.getRecords().size(); i++) {
			ExaminationPointDO obj = (ExaminationPointDO) page.getRecords().get(i);
			if (obj.getCreaterId() != null){
				UserDO userDO = userService.selectById(obj.getCreaterId());
				if (userDO != null) {
					obj.setCreaterName(userDO.getUsername());
				}
			}
			if (obj.getUserId() != null){
				UserDO userDO = userService.selectById(obj.getUserId());
				if (userDO != null) {
					obj.setUName(userDO.getUsername());
				}
			}
			if (obj.getOrgId() != null){
				OrganizationNewsDO organizationNewsDO = organizationNewsService.selectById(obj.getOrgId());
				if (organizationNewsDO != null) {
					obj.setOrgName(organizationNewsDO.getName());
				}
			}
			if (obj.getEbatchId() != null){
				ExaminationBatchDO examinationBatchDO = examinationBatchService.selectById(obj.getEbatchId());
				if (examinationBatchDO != null) {
					obj.setEbatchName(examinationBatchDO.getName());
				}
			}
			list.add(obj);
		}
		page.setRecords(list);
        return Result.ok(page);
	}
	
	@GetMapping("/add")
	@RequiresPermissions("jzweb:examinationPoint:add")
	String add(Model model){
		model.addAttribute("examinationBatch", examinationBatchService.selByAll());
		model.addAttribute("orgRole", organizationRolesService.selByRoleType(5));
	    return "jzweb/examinationPoint/add";
	}

	@GetMapping("/edit/{id}")
	@RequiresPermissions("jzweb:examinationPoint:edit")
	String edit(@PathVariable("id") Long id,Model model){
		model.addAttribute("examinationBatch", examinationBatchService.selByAll());
		model.addAttribute("orgRole", organizationRolesService.selByRoleType(5));
		ExaminationPointDO examinationPoint = examinationPointService.selectById(id);
		model.addAttribute("examinationPoint", examinationPoint);
	    return "jzweb/examinationPoint/edit";
	}
	
	@Log("添加考点")
	@ResponseBody
	@PostMapping("/save")
	@RequiresPermissions("jzweb:examinationPoint:add")
	public Result<String> save( ExaminationPointDO examinationPoint){
		examinationPoint.setCreaterId(this.getUserId());
		examinationPointService.insert(examinationPoint);
        return Result.ok();
	}
	
	@Log("修改考点")
	@ResponseBody
	@RequestMapping("/update")
	@RequiresPermissions("jzweb:examinationPoint:edit")
	public Result<String>  update( ExaminationPointDO examinationPoint){
		boolean update = examinationPointService.updateById(examinationPoint);
		return update ? Result.ok() : Result.fail();
	}
	
	@Log("删除考点")
	@PostMapping( "/remove")
	@ResponseBody
	@RequiresPermissions("jzweb:examinationPoint:remove")
	public Result<String>  remove( Long id){
		examinationPointService.deleteById(id);
        return Result.ok();
	}
	
	@Log("批量删除考点")
	@PostMapping( "/batchRemove")
	@ResponseBody
	@RequiresPermissions("jzweb:examinationPoint:batchRemove")
	public Result<String>  remove(@RequestParam("ids[]") Long[] ids){
		examinationPointService.deleteBatchIds(Arrays.asList(ids));
		return Result.ok();
	}
	
}
